package com.eightywork.life.activity;

import cn.jpush.android.api.JPushInterface;

import com.eightywork.life.R;
import com.eightywork.life.service.CacheService;
import com.umeng.analytics.MobclickAgent;

import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.ProgressBar;

public class BrowserActivity extends BaseActivity implements OnClickListener {
	private ImageButton leftButton, backButton, forwardButton, refreshButton,
			openoutButotn;
	private WebView webView;
	private String openUrl;
	private ProgressBar progressBar;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_browser);
		initView();
		initData();
	}
	
	@Override
	public void onResume() {		
		super.onResume();
		JPushInterface.onResume(this);
	    MobclickAgent.onResume(this);
	}
	
	@Override
	public void onPause() {
	    super.onPause();
		JPushInterface.onPause(this);
	    MobclickAgent.onPause(this);
	}


	void initView() {
		String title = getIntent().getStringExtra("title");
		if (title == null) {
			initHeader("附件详情");
		} else {
			initHeader(title);
		}
		leftButton = (ImageButton) findViewById(R.id.button_left);
		backButton = (ImageButton) findViewById(R.id.button_back);
		forwardButton = (ImageButton) findViewById(R.id.button_forward);
		refreshButton = (ImageButton) findViewById(R.id.button_refresh);
		openoutButotn = (ImageButton) findViewById(R.id.button_openout);
		progressBar = (ProgressBar) findViewById(R.id.progressBar);
		webView = (WebView) findViewById(R.id.webView1);
		leftButton.setOnClickListener(this);
		backButton.setOnClickListener(this);
		forwardButton.setOnClickListener(this);
		refreshButton.setOnClickListener(this);
		openoutButotn.setOnClickListener(this);
	}

	void initData() {
		String url = getIntent().getStringExtra("url");
		String idtype = getIntent().getStringExtra("idtype");
//		String append_id = getIntent().getStringExtra("append_id");
		webView.getSettings().setJavaScriptEnabled(true);
//		webView.setWebViewClient(new WebViewClient() {
//			
//		});
//	           openUrl = url;
//				+ "&fromclient=android&m_auth="
//				+ CacheService.getSpace().getmAuth();
//		openUrl = url + "&source/mobile/pages/goods_content.php?goodsid=479&m_auth=" + CacheService.getSpace().getmAuth();
//		showMessage("idtype--->" + idtype);
//		System.out.println("m_auth=" + CacheService.getSpace().getmAuth());
//		System.out.println("append_id" + append_id);
//		if (idtype.equals("feeid")) {   //账单
//			openUrl = url + "mobile/pages/bills_content.php?";
//		} else if (idtype.equals("couponid")) {   //优惠券
////			showMessage("优惠券");
//			openUrl = url + "mobile/pages/coupon_content.php?" + "&couponid=" + append_id;
//		} else if (idtype.equals("eventid")) {    //活动
//			openUrl = url + "mobile/pages/event_content.php?" + "&eventid=" + append_id;
//		} else if (idtype.equals("goodsid")) {    //商品
//			openUrl = url + "mobile/pages/goods_content.php?" + "&goodsid=" + append_id;
//		} else if (idtype.equals("housingid")) {    //租房
//			openUrl = url + "mobile/pages/housing_content.php?" + "&housingid=" + append_id;
//		} else if (idtype.equals("notifyid")) {   //通知
//			openUrl = url + "mobile/pages/notice_content.php?" + "&notifyid=" + append_id;
//		} else if (idtype.equals("secondhandid")) {   //二手
//			openUrl = url + "mobile/pages/secondhand_content.php?" + "&secondhandid=" + append_id;
//		} else if (idtype.equals("serviceitemid")) {     //服务
//			openUrl = url + "mobile/pages/service_content.php?" + "&itemid=" + append_id;
		if (idtype.equals("feed") || idtype.equals("ad")) {   //来自feed流
			openUrl = url;
		} else if (idtype.equals("mycoupon")) {     //我的优惠券
			openUrl = url + "mobile/pages/mycoupon.php?";
		}  else if (idtype.equals("myservice")) {     //我预约的服务
			openUrl = url + "mobile/pages/myservice.php?";
		}  else if (idtype.equals("myparty")) {     //我参与的活动
			openUrl = url + "mobile/pages/myevent.php?";	
		} else if (idtype.equals("systemnotify")) {
			openUrl = url;
		} else  {	
			showMessage("请求的链接不存在！");  //mymessage
			progressBar.setVisibility(View.INVISIBLE);
		} 
		if(CacheService.isLogined()){
			openUrl = openUrl + "&client=android" + "&m_auth=" + CacheService.getSpace().getmAuth(); 
		} else {
			openUrl = openUrl + "&client=android" + "&m_auth=" + "";
		}
		webView.setWebViewClient(new WebViewClient() {
			public boolean shouldOverrideUrlLoading(WebView view, String url) { // 重写此方法表明点击网页里面的链接还是在当前的webview里跳转，不跳到浏览器那边
				view.loadUrl(url);
				return false;
			}
			 @Override
             public void onPageFinished(WebView view, String url) 
             {
                 progressBar.setVisibility(View.INVISIBLE);
                 webView.setVisibility(View.VISIBLE);
                 refreshButton.setImageResource(R.drawable.browser_refresh_button);
                     super.onPageFinished(view, url);
             }
		});
		webView.setWebChromeClient(new WebChromeClient(){
			@Override 
	        public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {  
	            // 构建一个Builder来显示网页中的alert对话框  
	           Builder builder = new Builder(BrowserActivity.this);  
	           builder.setTitle("温馨提示");  
	           builder.setMessage(message);
	           builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						 result.confirm(); 
					}
	           	});
	            builder.setCancelable(false);  
	            builder.create();  
	            builder.show();  
	            return true;  
	        }  
	        
	        @Override 
	        public void onReceivedTitle(WebView view, String title) {  
	        	BrowserActivity.this.setTitle(title);  
	            super.onReceivedTitle(view, title);  
	        }  

	        // 处理Confirm事件  
	        @Override 
	        public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {  
	            Builder builder = new Builder(BrowserActivity.this);  
	            builder.setTitle("删除确认");  
	            builder.setMessage(message);
	            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						result.confirm();  
					}
				});

	            builder.setNeutralButton("取消", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						result.cancel(); 
					}
				});
	            builder.setCancelable(false);  
	            builder.create();  
	            builder.show();  
	            return true;  
	        }  
	        
	        // 处理提示事件  
	        @Override 
	        public boolean onJsPrompt(WebView view, String url, String message, String defaultValue,  
	                JsPromptResult result) {  
	            // 看看默认的效果  
	            return super.onJsPrompt(view, url, message, defaultValue, result);  
	        } 
	        
	        //设置网页加载的进度条
	        @Override
			public void onProgressChanged(WebView view, int newProgress){
	        	BrowserActivity.this.getWindow().setFeatureInt(Window.FEATURE_PROGRESS, newProgress * 100);
	        	super.onProgressChanged(view, newProgress);
	        }
		});
		System.out.println("openUrl--->" + openUrl);
		webView.loadUrl(openUrl);
		
	}

	@Override
	public void onClick(View v) {
		if (v == leftButton) {
			finish();
		} else if (v == backButton) {
			webView.goBack();
		} else if (v == forwardButton) {
			webView.goForward();
		} else if (v == refreshButton) {
			webView.reload();
		} else if (v == openoutButotn) {
			Intent intent = new Intent();        
			intent.setAction("android.intent.action.VIEW");    
			intent.setData(Uri.parse(openUrl));  
			startActivity(intent);
		}
	}
}
